# json
import json
# settings
from settings.settings import settings
# agv socket
from sockets.agvsocket import AgvSocket
# socketio
from sockets.frontsocket import sio
# settings
from settings.settings import settings
# timers
from timers.scheduler import MyScheduler

scheduler = MyScheduler().scheduler
agv_sio_instance = AgvSocket()


async def init_status_event():
    # scheduler.add_job(status_all2, 'interval', seconds=2, args=[], id='all2')
    scheduler.add_job(push_info, 'interval', seconds=2, args=[], id='push_info')
    scheduler.add_job(status_loc, 'interval', seconds=2, args=[], id='status_loc')


async def push_info():
    try:
        res = agv_sio_instance.receive(agv_sio_instance.get(settings.AGV_PORT_PUSH))
        await sio.emit('pushinfo', res)
        print('------------')
    except Exception as e:
        print('agv上推数据失败,服务器暂停接受数据')
        scheduler.pause_job('push_info')


async def status_loc():
    try:
        agv_sio_instance.send(agv_sio_instance.get(settings.AGV_PORT_STATUS), 1, 1004)
        res = agv_sio_instance.receive(agv_sio_instance.get(settings.AGV_PORT_STATUS))
        await sio.emit('status_loc', res)
    except Exception as e:
        print('查询agv位置信息失败')
        scheduler.pause_job('status_loc')


async def status_all2():
    try:
        agv_sio_instance.send(agv_sio_instance.get(settings.AGV_PORT_OTHER), 1, 6000, {
            "name": "block", "loop": False})
        await sio.emit('message', 'msg from agv')
    except Exception as e:
        print('发送失败,AGV断开')
        scheduler.remove_job('all2')


async def status_all3():
    try:
        agv_sio_instance.send(agv_sio_instance.get(settings.AGV_PORT_OTHER), 1, 6000, {
            "name": "block", "loop": False})
        await sio.emit('message', 'msg from agv')
    except Exception as e:
        print('发送失败,AGV断开')
        scheduler.remove_job('all3')


# 添加定时器任务
# async def add_status_schedulers(websocket):
#     scheduler.add_job(job_status_all2, 'interval',
#                       seconds=1, args=[websocket])

# scheduler.add_job(job_status_all2, 'interval',
#                   seconds=1, args=[websocket])
# scheduler.add_job(job_status_all3, 'interval',
#                   seconds=3, args=[websocket])


# async def job_status_all2(websocket):
#     agv_status_socket = agv_socket_factory.get(Port.STATUS.value)
#     agv_other_socket = agv_socket_factory.get(Port.OTHER.value)
#     try:
#         agv_status_socket.send(1, 1101)
#         res = agv_status_socket.receive()
#         await websocketmanager.broadcast_json({
#             "name": "all2",
#             "data": res
#         })
#         # 判断robot是否被阻挡
#         if res["blocked"]:
#             res = agv_other_socket.send(
#                 1, 6000, {"name": "block", "loop": False})
#             print(res)
#             print('bloc')
#     except Exception as e:
#         print('agv disconnect from all2')
#         # scheduler.pause()
#         agv_socket_factory.reconnect()


# async def job_status_all3(websocket):
#     agv_status_socket = agv_socket_factory.get(Port.STATUS.value)
#     try:
#         agv_status_socket.send(1, 1102)
#         res = agv_status_socket.receive()
#         await websocketmanager.broadcast_json({
#             "name": "all3",
#             "data": res
#         })
#     except Exception as e:
#         print('agv disconnect from all3')
#         # scheduler.pause()
#         agv_socket_factory.reconnect()
